// 源插件
import React, { Fragment, PureComponent } from 'react';
import { DateDiff, DateStamp, FormatnumberZero, Mover, NumberDivide } from '@/function.js';
import { Button, Card, Progress, Divider, Form, message, notification, Switch } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import StandardTablePro from '@/components/UpgradeStandardTable/StandardTablePro';
import { connect } from 'dva';
import ExportJsonExcel from '@/components/XLSXOUT/js-export-excel-custom';

@connect(({ astock }) => ({
  astock,
}))
@Form.create()
class AStock extends PureComponent {
  componentWillMount() {}

  /**
   *  Describe:当前状态
   *  Created by xjc on 2019/5/15
   */

  state = {
    listLoading: false, // 加载状态
    dataSource: {
      data: [],
      total: [],
      code: 0,
    },
    percent:0,
    list:0,
  };

  dataSourcechange() {
    const { dispatch } = this.props;
    const that = this;
    that.setState({
      dataSource: {
        data: [],
        total: [],
        code: 0,
      },
      percent:0,
    });
    dispatch({
      type: 'astock/GetSize',
      callback: res => {
        that.setState({
          list:res.list,
          status:'active',
        },()=>{
            that.getInfo(0);
        })
      }
    })

  }

getInfo(pageSize){
  const { dispatch } = this.props;
  const that = this;
  dispatch({
    type: 'astock/GetInfo',
    payload:pageSize,
    callback: res => {
      pageSize++;
      that.setState({
        percent:parseInt(window.mathService.mul(window.mathService.div(pageSize,that.state.list),100)),
        dataSource: {
          title: '筛选股票列表',
          code: 1,
          data: that.state.dataSource.data.concat(res.list),
          total: [],
          columns: [
            {
              title: '股票代码',
              dataIndex: 'code',
              type: 'string',
              search: true,
              render:(value,row,html)=>{
                return  <a href={"http://finance.sina.com.cn/realstock/company/"+ value.split(".").join("").toLowerCase() +"/nc.shtml?qq-pf-to=pcqq.c2c"} target="_blank">{value}</a>
              }
            },
            {
              title: '股票名字',
              dataIndex: 'name',
              type: 'string',
              search: true,
            },
          ],
        },
      },()=>{
        if(that.state.list !== pageSize){
          that.getInfo(pageSize);
        }else{
          that.setState({
            status:'success',
          })
        }
      })
    }
  })
}

  sheetItem(dataInfos, dataInfosColumns, sheetName) {
    let res = {};
    const infosTable = [];
    const infosFilter = [];
    const sheetHeader = [];
    const sheetWidth = [];
    if (dataInfosColumns) {
      for (const j in dataInfosColumns) {
        const item = dataInfosColumns[j];
        infosFilter.push(item.title);
        sheetHeader.push(item.title);
        sheetWidth.push({ wpx: 100 });
      }
    }
    if (dataInfos) {
      for (const i in dataInfos) {
        const dataInfosItem = dataInfos[i];
        if (dataInfos) {
          const obj = {};
          for (const j in dataInfosColumns) {
            const cItem = dataInfosColumns[j];
              obj[cItem.title] = dataInfosItem[cItem.dataIndex];
          }
          infosTable.push(obj);
        }
      }
    }
    res = {
      sheetData: infosTable,
      sheetName,
      sheetFilter: infosFilter,
      sheetHeader,
      columnWidths: sheetWidth,
    };
    return res;
  }
  outXlsx() {
    if(this.state.status==='success'){

    const { dataSource } = this.state;
    const option = {
      fileName: 'A股筛选导出',
      datas: [],
    };
    option.datas.push(this.sheetItem(dataSource.data, dataSource.columns, dataSource.title));
    const toExcel = new ExportJsonExcel(option);
    toExcel.saveExcel();

    }else{
      message.error('请先加载完所有数据')
    }
  }

  render() {
    return (
      <PageHeaderWrapper>
        <Card>
          <Progress percent={this.state.percent} status={this.state.status}/>
          <br/>
          <br/>
          <Button onClick={this.dataSourcechange.bind(this)}>重新筛选</Button>
          <Button type="primary" style={{ marginLeft: 25 }} onClick={this.outXlsx.bind(this)}>
            导出XLSX
          </Button>
          <br/>
          <br/>
          <StandardTablePro dataSource={this.state.dataSource} loadingTime = {15000}/>
        </Card>
      </PageHeaderWrapper>
    );
  }

  componentDidMount() {
    this.dataSourcechange()
  }

  componentWillUnmount() {}
}

export default AStock;
